package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.PersonnelInfo;
import com.ruoyi.personnel.service.IPersonnelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 人员基本信息Controller
 *
 * @author ruoyi
 * @date 2021-02-22
 */
@Api("人员基本信息")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/personnel/info" )
public class PersonnelInfoController extends BaseController {

    private final IPersonnelInfoService iPersonnelInfoService;

    /**
     * 查询人员基本信息列表
     */
    @ApiOperation("查询人员基本信息列表")
//    @PreAuthorize("@ss.hasPermi('personnel:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(PersonnelInfo personnelInfo) {
        startPage();
        List<PersonnelInfo> list = iPersonnelInfoService.queryList(personnelInfo);
        return getDataTable(list);
    }

    /**
     * 导出人员基本信息列表
     */
    @ApiOperation("导出人员基本信息列表")
//    @PreAuthorize("@ss.hasPermi('personnel:info:export')" )
    @Log(title = "人员基本信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(PersonnelInfo personnelInfo) {
        List<PersonnelInfo> list = iPersonnelInfoService.queryList(personnelInfo);
        ExcelUtil<PersonnelInfo> util = new ExcelUtil<PersonnelInfo>(PersonnelInfo.class);
        return util.exportExcel(list, "info" );
    }

    /**
     * 获取人员基本信息详细信息
     */
    @ApiOperation("获取人员基本信息详细信息")
//    @PreAuthorize("@ss.hasPermi('personnel:info:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iPersonnelInfoService.getById(id));
    }

    /**
     * 新增人员基本信息
     */
    @ApiOperation("新增人员基本信息")
//    @PreAuthorize("@ss.hasPermi('personnel:info:add')" )
    @Log(title = "人员基本信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PersonnelInfo personnelInfo) {
        // 创建者
        personnelInfo.setCreateBy(SecurityUtils.getUsername());
        if (iPersonnelInfoService.save(personnelInfo)) {
          return AjaxResult.success(personnelInfo.getId());
        } else {
          return AjaxResult.error();
        }
    }

    /**
     * 修改人员基本信息
     */
    @ApiOperation("修改人员基本信息")
//    @PreAuthorize("@ss.hasPermi('personnel:info:edit')" )
    @Log(title = "人员基本信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PersonnelInfo personnelInfo) {
        return toAjax(iPersonnelInfoService.updateById(personnelInfo) ? 1 : 0);
    }

    /**
     * 删除人员基本信息
     */
    @ApiOperation("删除人员基本信息")
//    @PreAuthorize("@ss.hasPermi('personnel:info:remove')" )
    @Log(title = "人员基本信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iPersonnelInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
